package com.xujiang.common_core.core;

import android.os.CountDownTimer;
import android.view.View;

/**
 * 倒计时的工具类
 *
 * @author xujiang
 * @version 2017-05-05 10:36
 */
public class MCountDownTimer extends CountDownTimer {

    private View view;

    public static MCountDownTimer newMCountDownTimer(long millisInFuture, long countDownInterval, View view){
        millisInFuture = millisInFuture + countDownInterval/2;  //解决最后一秒无回掉
        return new MCountDownTimer(millisInFuture,countDownInterval,view);
    }
    /**
     * @param millisInFuture 表示以毫秒为单位 倒计时的总数
     *
     * 例如 millisInFuture=1000 表示1秒
     * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
     *
     * 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
     */
    private MCountDownTimer(long millisInFuture, long countDownInterval, View view) {
        super(millisInFuture, countDownInterval);
        this.view = view;
    }


    @Override
    public void onFinish() {
    //    button.setText(text);
    //    button.setEnabled(true);
    //    button.setBackgroundResource(R.drawable.btn_base_dark);  //设置成可点击的样式
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (millisUntilFinished / 1000 > 60) {
            long h = millisUntilFinished / 1000 / 60 / 60;
            long m = millisUntilFinished / 1000 / 60 % 60;
            long s = millisUntilFinished / 1000 % 60;
            //view.setText("Available in " + h + " h " + m + " m " + s + " s.");
        } else {
           // button.setText("Available in " + millisUntilFinished / 1000 + " second.");
        }
    }
}
